package com.example.meimessage.newmsg;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.ActionBar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.SimpleAdapter;

import com.example.meimessage.R;
import com.example.meimessage.favorite.GridviewAdapter;

public class AttachFavoriteMore extends Activity{
	private GridView gridView;
	private EditText searchEdit;
	private ImageView searchImage;
	private SimpleAdapter adapter;
	private Intent intent;
	private ActionMode searchActionMode;
	private ArrayList<HashMap<String ,Object>> items=new ArrayList<HashMap<String ,Object>>();
	private ArrayList<HashMap<String,Object>> search_items=new ArrayList<HashMap<String ,Object>>();
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.favorite_more);
		intent=getIntent();
		ActionBar bar=getActionBar();
		bar.setTitle(intent.getStringExtra("title"));
		bar.setDisplayShowHomeEnabled(true);
		bar.setDisplayHomeAsUpEnabled(true);
		bar.setIcon(R.drawable.mx);
		gridView=(GridView) findViewById(R.id.mygridview);
		gridView.setOnItemClickListener(new gridViewListener());
		File file=new File(intent.getStringExtra("path"));
		File []files=file.listFiles();
		if(files!=null){
			for(int i=0;i<files.length;i++){
				HashMap<String,Object> map=new HashMap<String,Object>();
				map.put("gridView", files[i].getName().toString());
				items.add(map);
			}
		}
		init();
		startSearchActionMode();
	}
	public class gridViewListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			Intent intent=new Intent();
			if(search_items.isEmpty()){
				intent.putExtra("text", items.get(position).get("gridView").toString());
				setResult(1, intent);
				finish();
			}
			else{
				intent.putExtra("text", search_items.get(position).get("gridView").toString());
				setResult(1, intent);
				finish();
			}
		}

	}
	public void init() {
		GridviewAdapter myAdapter=new GridviewAdapter(items,this);
		gridView.setAdapter(myAdapter);
	}
	public void startSearchActionMode() {
		if (searchActionMode == null) {
            ActionMode.Callback cb = new searchModeCallback();
            searchActionMode = startActionMode(cb);
        }
	}
	public class searchModeCallback implements ActionMode.Callback {
        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
        	LayoutInflater factory = LayoutInflater.from(AttachFavoriteMore.this);
        	final View view = factory.inflate(R.layout.searchbox, null);
        	mode.setCustomView(view);
        	//getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
        	searchEdit=(EditText) view.findViewById(R.id.searchbox_edit);
        	searchImage=(ImageView) view.findViewById(R.id.searchbox_image);
        	searchImage.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					searchEdit.setText("");
					searchImage.setImageResource(R.drawable.touming);
				}
			});
        	searchEdit.addTextChangedListener(new TextWatcher() {
        		
				@Override
				public void onTextChanged(CharSequence s, int start, int before, int count) {
					if(searchEdit.getText().equals(""))
						searchImage.setImageResource(R.drawable.touming);
				}	
				@Override
				public void beforeTextChanged(CharSequence s, int start, int count,
						int after) {
				}				
				@Override
				public void afterTextChanged(Editable s) {
					
					if(searchEdit.getText().equals("")==false){
						searchImage.setImageResource(R.drawable.ic_clear_search_api_holo_light);
					}
					
					search_items.clear();
					for(int i=0;i<items.size();i++){
						for(int t=0;t<items.get(i).get("gridView").toString().length();t++){
							if(items.get(i).get("gridView").toString().startsWith(searchEdit.getText()
									.toString(),t)){
								HashMap<String,Object> map=new HashMap<String ,Object>();
								map.put("gridView", items.get(i).get("gridView").toString());
								search_items.add(map);
								break;
							}
						}
					}
					for(int i=0;i<search_items.size()-1;i++){
						for(int t=0;t<search_items.size()-i-1;t++){
							if(search_items.get(t).get("gridView").toString().length()>search_items.get(t+1).get("gridView")
									.toString().length()){
								String temp=search_items.get(t).get("gridView").toString();
								search_items.get(t).put("gridView", search_items.get(t+1).get("gridView").toString());
								search_items.get(t+1).put("gridView", temp);
							}
						}
					}
					adapter=new SimpleAdapter(AttachFavoriteMore.this, search_items, R.layout.favorite_more_gridview,
							new String[]{"gridView"},
							new int[]{R.id.gridView_item});
					gridView.setAdapter(adapter);
				}
			});
            return true;
        }
        @Override
        public void onDestroyActionMode(ActionMode mode) {
            searchActionMode = null;
            AttachFavoriteMore.this.finish();
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }
		@Override
		public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
			return false;
		}
    }

}
